package vn.fpt.snake.dataAccess;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;

import vn.fpt.snake.commons.Constants;
import vn.fpt.snake.commons.Resource;
import vn.fpt.snake.entity.GameStatus;
import vn.fpt.snake.entity.Settings;
import vn.fpt.snake.entity.HighScore;

public class DataAccess {
	public DataAccess() {

	}

	public Settings getSettings() {
		return Resource.settings;
	}

	public void saveSettings() {
		throw new UnsupportedOperationException();
	}

	public GameStatus getGameStatus() {
		return Resource.gameStatus;
	}

	public void saveGameStatus() {
		throw new UnsupportedOperationException();
	}

	public List<HighScore> getHighScores() throws Exception {
		List<HighScore> highScores = new ArrayList<>();
		InputStream inputStream = null;
		BufferedReader reader = null;
		try {
			inputStream = new FileInputStream(getFile(Constants.HIGH_SCORE_FILE));
			if (inputStream != null) {
				reader = new BufferedReader(new InputStreamReader(inputStream));
				String line;
				int counter = 0;
				while ((line = reader.readLine()) != null && counter < getSettings().getMaxNumberHighScore()) {
					String[] stringParts = line.split("\t");
					String name = stringParts[0];
					String score = stringParts[1];
					highScores.add(new HighScore(name, Integer.parseInt(score)));
					counter++;
				}
			}
		} finally {
			if (reader != null)
				reader.close();
			if (inputStream != null)
				inputStream.close();
		}
		return highScores;
	}

	public void saveHighScores(List<HighScore> highScores) throws Exception {
		if (highScores != null) {
			FileOutputStream fileOutputStream = null;
			PrintWriter writer = null;
			try {
				fileOutputStream = new FileOutputStream(
						getFile(Constants.HIGH_SCORE_FILE));
				writer = new PrintWriter(fileOutputStream, true);
				for (HighScore highScore : highScores) {
					writer.println(String.format(Constants.HIGH_SCORE_STANDARD,
							highScore.getName(), highScore.getScore()));
				}
			} catch (Exception e) {
				// TODO: handle exception
				throw e;
			} finally {
				if (writer != null) {
					writer.flush();
					writer.close();
				}
				if (fileOutputStream != null) {
					fileOutputStream.flush();
					fileOutputStream.close();
				}			
			}
		}

	}

	
	public Image getImage(String name) throws Exception {
		String path = getClass().getProtectionDomain().getCodeSource()
				.getLocation().getPath();
		ImageIcon imageIcon = new ImageIcon(path + "resource" + name);
		return imageIcon.getImage();
	}

	public File getFile(String name) throws Exception {
		String path = getClass().getProtectionDomain().getCodeSource()
				.getLocation().getPath();
		return new File(path + "resource" + name);
	}

	public List<String> getTextFromFile(File file) throws Exception {
		List<String> texts = new ArrayList<>();
		InputStream inputStream = null;
		BufferedReader reader = null;
		try {
			inputStream = new FileInputStream(file);
			if (inputStream != null) {
				reader = new BufferedReader(new InputStreamReader(inputStream));
				String line;
				while ((line = reader.readLine()) != null) {
					texts.add(line);
				}
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (reader != null)
				reader.close();
			if (inputStream != null)
				inputStream.close();
		}
		return texts;
	}
}
